<template>
  <div id="promotion-shopInformation">
    <div  class="main-container-views">
      <div class="el-card box-card is-always-shadow">
        <div class="el-card__body">
          <h3>商铺信息</h3>
          <el-form
            :model="tableData"
            ref="tableData"
            label-width="150px"
            class="demo-ruleForm"
          >
            <el-form-item
              label="标题"
              prop="title"
              :rules="[{ required: true, message: '商铺标题不能为空' , trigger: 'blur' }]"
            >
              <el-input
                v-model.number="tableData.title"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="商铺logo">
              <el-upload
                class="avatar-uploader"
                action="http://192.168.1.99:8080/upload/admin"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img
                  v-if="tableData.logoUrl"
                  :src="tableData.logoUrl"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item
              label="描述"
              prop="description"
              :rules="[{ required: true, message: '商铺描述不能为空', trigger: 'blur'}]"
            >
              <el-input
                v-model.number="tableData.description"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="地址"
              prop="address"
              :rules="[{ required: true, message: '商铺地址不能为空', trigger: 'blur'  }]"
            >
              <el-input
                v-model.number="tableData.address"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="button">
        <button
          type="button"
          class="el-button el-button--primary"
          @click="submitForm('tableData')"
           v-permission="'promote:merchant:update'"
        >
          <span>保存更改</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import merchant from "@/api/merchant";
export default {
  data() {
    return {
      imageUrl: "",
      tableData: {},
    };
  },
  created: function() {
    //一开始的渲染数据
    merchant.getMerchant().then((res) => {
      this.tableData = res.data.data;
    });
  },
  methods: {
    submitForm(formName) {
      console.log(this.tableData);
      let that =this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          merchant.updateMerchant(this.tableData).then((res) => {
            console.log(res)
            if (res.data.errmsg == "成功") {
            that.topTips(res.data.errmsg,"success")
          } else {
            that.topTips(res.data.errmsg,"warning")
          }
          });
        } else {
          return false;
        }
      });
    },
    handleAvatarSuccess(res) {
      console.log(res)
      this.tableData.logoUrl = res.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
#promotion-shopInformation {
  height: 100%;
  padding: 20px;
}
h3 {
  line-height: 25px;
}
.button {
  line-height: 40px;
  margin-top: 10px;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: hsl(215, 8%, 58%);
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
::-webkit-scrollbar {
  /*对话框的隐藏滚动条但是可以滚动，滚动条的宽度*/
  width: 0px !important;
}
</style>
